package com.ftg.learn.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author kangjx
 * @date 2024/1/30
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ToWeb<T> {

    private Integer code;

    private String msg;

    /**
     * 数据
     * JsonInclude(JsonInclude.Include.NON_NULL) 用于处理当数据为null时，不返回该字段
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public static <T> ToWeb<T> success(T data) {
        return new ToWeb<T>(200, "success", data);
    }

    public static <T> ToWeb<T> success(String msg, T data) {
        return new ToWeb<T>(200, msg, data);
    }

    public static <T> ToWeb<T> failure(T data) {
        return new ToWeb<T>(500, "failure", data);
    }

    public static <T> ToWeb<T> failure(String msg, T data) {
        return new ToWeb<T>(500, msg, data);
    }

}
